package com.rad.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.rad.dao.DBPool;
import com.rad.dao.UserDaoITF;
import com.rad.po.TabUser;

public class UserDaoImpl implements UserDaoITF {

	@Override
	public TabUser getTabUser(String loginname, String pwd) {
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		try
		{
			conn = DBPool.getConnection();
			String sql = "SELECT * FROM TAB_USER WHERE loginName=? AND loginPwd=?";
			statement = conn.prepareStatement(sql);
			statement.setString(1, loginname);
			statement.setString(2, pwd);
			rs = statement.executeQuery();
			if (rs.next())
			{
				TabUser user = new TabUser();
				user.setLoginName(rs.getString("loginName"));
				user.setLoginPwd(rs.getString("loginPwd"));
				user.setTrueName(rs.getString("trueName"));
				user.setMobile(rs.getString("mobile"));
				user.setDepartment(rs.getString("department"));
				return user;
			}
			else
			{
				return null;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			DBPool.close(conn, statement, rs);
		}
		return null;
	}

	@Override
	public void changeUserPassword(String loginName, String newPassword) {
		Connection conn = null;
		PreparedStatement statement = null;
		try
		{
			conn = DBPool.getConnection();
			conn.setAutoCommit(false);
			String sql = "UPDATE TAB_USER SET loginPwd=? WHERE loginName=?";
			statement = conn.prepareStatement(sql);
			statement.setString(1, newPassword);
			statement.setString(2, loginName);
			statement.executeUpdate();
			conn.commit();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			DBPool.close(conn, statement, null);
		}
	}
}
